import {Component, ViewChild} from '@angular/core';
import {Content, IonicPage, NavController, NavParams, ViewController} from 'ionic-angular';
import {Util} from "@providers/util";
import {DialogProvider} from "@providers/dialog";
import {StorageProvider} from "@providers/storage";
import {HttpProvider} from "@providers/http";

/**
 * Generated class for the WorkerLookDelayreasonPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-look-delayreason',
  templateUrl: 'worker-look-delayreason.html',
})
export class WorkerLookDelayreasonPage {

  @ViewChild(Content) content: Content;
  isScrollDown = false;//判断页面是否是向下滑动
  name: any;
  phone: number;
  address: any;
  number: number;
  userInfo: any;
  QUANTITY: number;
  data: any = {"YYSJ": '', "reason": '', "SKZT": '', "FWXZ": ''};
  typeDataArr: any;
  typeData: any;
  datas: any = this.navParams.data;

  min: string = Util.getDate(0);
  max: string = Util.getDate(365);

  constructor(public navCtrl: NavController, public navParams: NavParams,
              private storage: StorageProvider, private dialog: DialogProvider,
              private viewCtrl: ViewController,
              private http: HttpProvider) {
    this.userInfo = this.storage.read("userInfo");
    this.getCard();
    this.getBranch();
  }

  ionViewDidLoad() {
    this.number = this.navParams.data.ID;
    this.name = this.navParams.data.NAME;
    this.phone = this.navParams.data.MOBILE;
    this.address = this.navParams.data.ADDRESS;
    console.info(this.navParams)
  }

  dismiss(data?: any) {
    if (data) {
      this.viewCtrl.dismiss(data);
      return;
    }
    this.viewCtrl.dismiss();
  }

  CheckData() {
    if (Util.isNull(this.data.SKZT)) {
      this.dialog.showAlert('收卡状态不能为空');
      return;
    }
    if (Util.isNull(this.data.FWXZ)) {
      this.dialog.showAlert('服务性质不能为空');
      return;
    }
    if (Util.isNull(this.data.YYSJ)) {
      this.dialog.showAlert('日期不能为空');
      return;
    }
    if (Util.isNull(this.data.reason)) {
      this.dialog.showAlert('原因不能为空');
      return;
    }

    this.dialog.showLoading();
    this.http.post("/Inbound/invoke.do", {
      interfaceId: 'csm.som.cssOrder.makeSorderYCFW',
      data: JSON.stringify({
        "CZYMC": this.userInfo.username, "YYSJ": this.data.YYSJ,
        "GD01": this.datas.ID, "BZ": this.data.reason, "SKZT": this.data.SKZT, "FWXZ": this.data.FWXZ
      })
    }).subscribe(message => {
      this.dialog.hideLoading();
      // this.datas.splice(this.datas.i, 1);
    }, error => {
      this.dialog.hideLoading();
      this.dialog.showAlert(error);
    });

    this.dismiss(this.data);
  }

  getBranch() {
    this.http.post("/Inbound/invoke.do",
      {
        interfaceId: "csm.css.jcdy.qcfwquery.selectenum",
        data: JSON.stringify({"TYPE": "CSS_FWXZ"})
      }).subscribe(
      message => {
        console.info(message.data);
        this.typeDataArr = message.data;
      },
      error => {
        this.dialog.showAlert("请选择服务性质");
      });
  }

  getCard() {
    this.http.post("/Inbound/invoke.do",
      {
        interfaceId: "csm.css.jcdy.qcfwquery.selectenum",
        data: JSON.stringify({"TYPE": "CSS_SKZT"})
      }).subscribe(
      message => {
        console.info(message.data);
        this.typeData = message.data;
      },
      error => {
        this.dialog.showAlert("请选择收卡状态");
      });
  }

  /**
   * 滑动时触发
   */
  scrollContent() {
    this.isScrollDown = this.content.directionY == "down";
  }

}
